﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KeywordAuction
{
    public class SimulationAuctionHost : AuctionHost , ICloneable
    {

        public SimulationAuctionHost ParentAuctionHost { get; set; }
        public List<SimulationAuctionHost> ChildAuctionHost { get; set; }
        public Advertiser ActorAdvertiser { get; set; }

        public SimulationAuctionHost()
        {
            ChildAuctionHost = new List<SimulationAuctionHost>();
        }

        //public SimulationAuctionHost(AuctionHost host)
        //{
        //   this.Adspots = host.Adspots.ToList();
        //   this.Advertisers= host.Advertisers.ToList();
        //   this.AuctionMechanism = host.AuctionMechanism;
        //}

        public new object Clone()
        {
            var auctionHost = new SimulationAuctionHost();
            auctionHost.ParentAuctionHost = this;
            auctionHost.AuctionMechanism = this.AuctionMechanism;
            auctionHost.ActorAdvertiser = this.ActorAdvertiser;
            this.ChildAuctionHost.Add(auctionHost);


            foreach (var item in Adspots)
            {
                var adspot = auctionHost.CreateAdspot();
                adspot.ClickThroughtRates = item.ClickThroughtRates.ToList();
                adspot.Traffic = item.Traffic;
                adspot.Id = item.Id;

                auctionHost.Adspots.Add(adspot);
            }
            foreach (var item in this.Advertisers)
            {
                var advertiser = new Advertiser();
                advertiser.Tactics = item.Tactics;
                advertiser.Strategies = item.Strategies.ToList();
                advertiser.AttendingAuction = this;
                advertiser.CurrentStrategy = item.CurrentStrategy;
                advertiser.Id = item.Id;
                advertiser.BudgetConstraint = item.BudgetConstraint;
                foreach (var bid in item.Biddings)
                {
                    var adspot = auctionHost.Adspots.Where(p => bid.AttendingAdspot.Id == p.Id).FirstOrDefault();
                    var newBid = AdvertiserBidding.CreateAdvertiserBidding(advertiser, adspot);
                    newBid.ExpectedRevenue = bid.ExpectedRevenue;
                    newBid.Bid = bid.Bid;
                    adspot.Bitting.Add(newBid);
                    advertiser.Biddings.Add(newBid);
                }
                auctionHost.Advertisers.Add(advertiser);
            }
            return auctionHost;
        }
    }
}
